﻿namespace FeedbackBrowser
{
    using System;
    using System.Globalization;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media.Animation;
    using System.Windows.Media.Imaging;

    using ExtendedWindowsControls;

    using Microsoft.TeamFoundation.Client;
    using Microsoft.TeamFoundation.Framework.Client;
    using Microsoft.TeamFoundation.Framework.Common;
    using Microsoft.TeamFoundation.WorkItemTracking.Client;

    public partial class Backup
    {
        #region Fields

        private readonly ExtendedNotifyIcon _extendedNotifyIcon;
        private readonly Storyboard _gridFadeInStoryBoard;
        private readonly Storyboard _gridFadeOutStoryBoard;

        #endregion Fields

        #region Methods

        /// <summary>
        private void LoadFeedbackRequests()
        {
            try
            {
                Uri collectionUri;

                if (string.IsNullOrEmpty(Properties.Settings.Default.TfsCollectionUri))
                {
                    var tpp = new TeamProjectPicker(TeamProjectPickerMode.SingleProject, false, new UICredentialsProvider());

                    tpp.ShowDialog();

                    if (tpp.SelectedTeamProjectCollection != null)
                    {
                        Properties.Settings.Default.TfsCollectionUri = "https://" +
                                                                       tpp.SelectedTeamProjectCollection.Name.Replace(
                                                                           '\\', '/');
                        Properties.Settings.Default.TfsProjectName = tpp.SelectedProjects[0].Name;

                        collectionUri = new Uri(Properties.Settings.Default.TfsCollectionUri);
                        UICredentialsProvider provider = new UICredentialsProvider();
                        TfsTeamProjectCollection tfs1 = new TfsTeamProjectCollection(collectionUri, provider);
                        tfs1.Authenticate();
                        //IAccessControlService acs = tfs1.GetService<IAccessControlService>();
                        //ServiceIdentity defaultServiceIdentity = acs.ProvisionServiceIdentity();
                        //Properties.Settings.Default.Username = defaultServiceIdentity.IdentityInfo.Name;
                        //Properties.Settings.Default.Password = defaultServiceIdentity.IdentityInfo.Password;
                        Properties.Settings.Default.Save();

                        //try
                        //{
                        //    CredentialsCacheManager.StoreCredentials(collectionUri,
                        //                                             defaultServiceIdentity.IdentityInfo.Name,
                        //                                             defaultServiceIdentity.IdentityInfo.Password,
                        //                                             CachedCredentialsType.ServiceIdentity,
                        //                                             true);
                        //}
                        //catch (Exception ex)
                        //{
                        //    MessageBox.Show(ex.ToString());
                        //}
                    }
                }
                collectionUri = new Uri(Properties.Settings.Default.TfsCollectionUri);
                //else
                //{

                //TfsClientCredentials prov =
                //    new TfsClientCredentials(new SimpleWebTokenCredential("latekpro@hotmail.com",
                //                                                          "Elephant@34"));
                //TfsTeamProjectCollection tfs2 = new TfsTeamProjectCollection(collectionUri, prov);

                //tfs2.Authenticate();

                //// Get the TFS Identity Management Service
                //IIdentityManagementService identityManagementService = tfs2.GetService<IIdentityManagementService>();

                //// Look up the user that we want to impersonate
                //TeamFoundationIdentity identity = identityManagementService.ReadIdentity(IdentitySearchFactor.AccountName, Properties.Settings.Default.Username, MembershipQuery.None, ReadIdentityOptions.None);

                //tfs2 = new TfsTeamProjectCollection(collectionUri, prov, identity.Descriptor);

                //}

                //try
                //{
                //    CredentialsCacheManager.StoreCredentials(collectionUri,
                //                                             Properties.Settings.Default.Username,
                //                                                          Properties.Settings.Default.Password,
                //                                             CachedCredentialsType.ServiceIdentity,
                //                                             true);
                //}
                //catch (Exception ex)
                //{
                //    MessageBox.Show(ex.ToString());
                //}

                var tfs2 = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(collectionUri);
                tfs2.EnsureAuthenticated();
                var wist = (WorkItemStore)tfs2.GetService(typeof(WorkItemStore));
                Project tp = wist.Projects[Properties.Settings.Default.TfsProjectName];

                var fReq = @"SELECT [System.Id], [System.WorkItemType]," +
                           "  [System.State], [System.AssignedTo], [System.Title] " +
                           "  FROM WorkItems " +
                           "  WHERE [System.TeamProject] = '" + tp.Name +
                           "' AND [System.WorkItemType]='Feedback Request' and [System.AssignedTo] =@me ORDER BY [System.WorkItemType], [System.Id]";

                var queryFeedbacks = new Query(wist, fReq);
                var feedbacks = queryFeedbacks.RunQuery();

                // 2. Get the Uri of the session from the bug
                foreach (WorkItem bug in feedbacks)
                {
                }

            }

            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }
        }

        #endregion Methods
    }
}